package com.newear.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/3 0003.
 */

public class ReportInfo implements Serializable{
    // 健康分数，用于显示“亚健康外框颜色”，还有下面方块个数
    public int healthScore;
    public String displayTime;
    public String time;
    public List<Symptom> symptomList;
    public List<HealthProduct> healthProductList;
    public Solutions solutions;
    // 2.亚健康
    public String healthStatusText;
    public FaceResult faceResult;
    public int healthStatusIndex;
    public int id;
    public String shareUrl;
    public String faceThumbUrl;
    // 查看分析-舌色分析
    public TongueResult tongueResult;
    //监测数值--面色值 0-255
    public FaceScoreDetail faceScoreDetail;
    public TongueScoreDetail tongueScoreDetail;

    public class Symptom implements Serializable{
        public String name;
        public int score;
    }

    public class HealthProduct {
        public String image;
        public String price;
        public String name;
        public String description;
    }

    public class Solutions implements Serializable{
        // 保健原则
        public List<String> healthGuidances;
        // 健康状态
        public List<SymptomDefinition> symptomDefinitions;
        // 中医功法
        public List<Video> videos;
        // 穴位按压
        public List<Massage> massages;
        public String assetPrefix;
    }

    public class Video implements Serializable{
        public String asset;
        public String name;
        public String symptom;
    }

    public class Massage implements Serializable{
        public String asset;
        public String content;
        public String symptom;
    }


    // 健康状态，点击状态按钮，定义、病因
    public class SymptomDefinition implements Serializable{
        public String name;
        public String define;
        public List<String> symptoms;
    }

    public class FaceResult implements Serializable{
        public int lipColor;
        public int faceColor;
        public int lipDetectRes;
        public int faceDetectRes;
        public int faceGloss;
        public String faceThumbUrl;
    }

    public class TongueResult implements Serializable{
        public int tongueDetectRes;
        public int tongueCrack;
        public int tongueFatThin;
        public int tongueNatureColor;
        public int tongueCoatThickness;
        public int tongueCoatColor;
        public String tongueThumbUrl;
    }

    public class FaceScoreDetail implements Serializable{
        public int faceL;
        public int faceA;
        public int faceB;
    }
    public class TongueScoreDetail implements Serializable{
        public int tongueL;
        public int tongueA;
        public int tongueB;
    }

    public boolean parse(JSONObject info) {
        if (info == null) {
            return false;
        }
        healthScore = info.optInt("health_score");
        displayTime = info.optString("display_time");
        time = info.optString("time");
        healthStatusText = info.optString("health_status_text");
        healthStatusIndex = info.optInt("health_status_index");
        id = info.optInt("id");
        shareUrl = info.optString("share_url");
        faceThumbUrl = info.optString("face_thumb_url");
        if (info.has("health_product")) {
            JSONArray healthJsonArr = info.optJSONArray("health_product");
            if (healthJsonArr != null && healthJsonArr.size() > 0) {
                healthProductList = new ArrayList<>();
                for (int i = 0; i < healthJsonArr.size(); i++) {
                    JSONObject healthObj = (JSONObject) healthJsonArr.get(i);
                    HealthProduct healthProduct = new HealthProduct();
                    healthProduct.name = healthObj.optString("name");
                    healthProduct.image = healthObj.optString("image");
                    healthProduct.description = healthObj.optString("description");
                    healthProduct.price = healthObj.optString("price");
                    healthProductList.add(healthProduct);
                }
            }
        }

        // 一定要先解
        if (info.has("symptoms")) {
            JSONArray symotomArr = info.optJSONArray("symptoms");
            if (symotomArr != null && symotomArr.size() > 0) {
                symptomList = new ArrayList<>();
                for (int i = 0; i < symotomArr.size(); i++) {
                    JSONObject symotomObj = (JSONObject) symotomArr.get(i);
                    Symptom symptom = new Symptom();
                    symptom.name = symotomObj.optString("name");
                    symptom.score = symotomObj.optInt("score");
                    symptomList.add(symptom);
                }
            } else {
                symptomList = new ArrayList<>();
                Symptom symptom = new Symptom();
                symptom.name = "健康";
                symptomList.add(symptom);
            }
        }

        if (info.has("solutions")) {
            JSONObject solutionObj = info.optJSONObject("solutions");
            if (solutionObj != null) {
                solutions = new Solutions();
                // 点击健康状态按钮，定义、病因，
                if (solutionObj.has("symptom_definition")) {
                    JSONObject symptomDefinitionObj = solutionObj.optJSONObject("symptom_definition");
                    if (symptomDefinitionObj != null) {
                        if (symptomList != null && symptomList.size() > 0) {
                            List<SymptomDefinition> symptomDefinitions = new ArrayList<>();
                            for (int i = 0; i < symptomList.size(); i++) {
                                String name = symptomList.get(i).name;
                                JSONObject jsonObject = symptomDefinitionObj.optJSONObject(name);
                                if (jsonObject != null) {
                                    SymptomDefinition symptomDefinition = new SymptomDefinition();
                                    symptomDefinition.name = jsonObject.optString("name");
                                    if (jsonObject.has("content")) {
                                        JSONArray contentArr = jsonObject.optJSONArray("content");
                                        if (contentArr != null) {
                                            List<String> symptoms = new ArrayList<String>();
                                            for (int j = 0; j < contentArr.length(); j++) {
                                                if (j == 0) {
                                                    symptomDefinition.define = (String) contentArr.get(0);
                                                } else {
                                                    symptoms.add((String) contentArr.get(j));
                                                }
                                            }
                                            symptomDefinition.symptoms = symptoms;
                                        }
                                    }
                                    symptomDefinitions.add(symptomDefinition);
                                }

                            }
                            solutions.symptomDefinitions = symptomDefinitions;
                        }
                    }
                }
                // 保健方案--保健原则
                if (solutionObj.has("health_guidance")) {
                    JSONObject healthGuidanceObj = solutionObj.optJSONObject("health_guidance");
                    if (healthGuidanceObj != null) {
                        if (symptomList != null && symptomList.size() > 0) {
                            List<String> healthGuidances = new ArrayList<>();
                            for (int i = 0; i < symptomList.size(); i++) {
                                String name = symptomList.get(i).name;
                                JSONObject jsonObject = healthGuidanceObj.optJSONObject(name);
                                if (jsonObject != null) {
                                    if (jsonObject.has("content")) {
                                        JSONArray contentArr = jsonObject.optJSONArray("content");
                                        if (contentArr != null) {
                                            for (int j = 0; j < contentArr.length(); j++) {
                                                healthGuidances.add((String) contentArr.get(j));
                                            }
                                        }
                                    }
                                }
                            }
                            solutions.healthGuidances = healthGuidances;
                        }
                    }
                }
                // 中医功法
                if (solutionObj.has("video")) {
                    JSONObject videoJson = solutionObj.optJSONObject("video");
                    if (videoJson != null) {
                        if (symptomList != null && symptomList.size() > 0) {
                            List<Video> videos = new ArrayList<>();
                            for (int i = 0; i < symptomList.size(); i++) {
                                String name = symptomList.get(i).name;
                                JSONObject jsonObject = videoJson.optJSONObject(name);
                                if (jsonObject != null) {
                                    Video video = new Video();
                                    video.symptom = jsonObject.optString("symptom");
                                    video.asset = jsonObject.optString("asset");
                                    video.name =  jsonObject.optString("name");
                                    videos.add(video);
                                }

                            }
                            solutions.videos = videos;
                        }
                    }
                }

                // 穴位按压
                if (solutionObj.has("massage")) {
                    JSONObject massageJson = solutionObj.optJSONObject("massage");
                    if (massageJson != null) {
                        if (symptomList != null && symptomList.size() > 0) {
                            List<Massage> massages = new ArrayList<>();
                            for (int i = 0; i < symptomList.size(); i++) {
                                String name = symptomList.get(i).name;
                                JSONObject jsonObject = massageJson.optJSONObject(name);
                                if (jsonObject != null) {
                                    Massage massage = new Massage();
                                    massage.symptom = jsonObject.optString("symptom");
                                    massage.asset = jsonObject.optString("asset");
                                    massage.content =  jsonObject.optString("content");
                                    massages.add(massage);
                                }
                            }
                            solutions.massages = massages;
                        }
                    }
                }

                //  前缀
                if (solutionObj.has("asset_prefix")) {
                    solutions.assetPrefix = solutionObj.optString("asset_prefix");
                }
            }
        }

        if (info.has("face_score_detail")) {
            JSONObject faceScoreDetailJson = info.optJSONObject("face_score_detail");
            if (faceScoreDetailJson != null) {
                faceScoreDetail = new FaceScoreDetail();
                faceScoreDetail.faceL = faceScoreDetailJson.optInt("face_L");
                faceScoreDetail.faceA = faceScoreDetailJson.optInt("face_a");
                faceScoreDetail.faceB = faceScoreDetailJson.optInt("face_b");
            }
        }

        if (info.has("tongue_score_detail")) {
            JSONObject faceScoreDetailJson = info.optJSONObject("tongue_score_detail");
            if (faceScoreDetailJson != null) {
                tongueScoreDetail = new TongueScoreDetail();
                tongueScoreDetail.tongueL = faceScoreDetailJson.optInt("nature_L");
                tongueScoreDetail.tongueA = faceScoreDetailJson.optInt("nature_a");
                tongueScoreDetail.tongueB = faceScoreDetailJson.optInt("nature_b");
            }
        }

        if (info.has("face_result")) {
            JSONObject faceResultObj = info.optJSONObject("face_result");
            if (faceResultObj != null) {
                faceResult = new FaceResult();
                faceResult.lipColor = faceResultObj.optInt("lipColor");
                faceResult.faceColor = faceResultObj.optInt("faceColor");
                faceResult.lipDetectRes = faceResultObj.optInt("lipDetectRes");
                faceResult.faceDetectRes = faceResultObj.optInt("faceDetectRes");
                faceResult.faceGloss = faceResultObj.optInt("faceGloss");
                faceResult.faceThumbUrl = faceResultObj.optString("faceThumbUrl");
            }
        }

        if (info.has("tongue_result")) {
            JSONObject tongueResultObj = info.optJSONObject("tongue_result");
            if (tongueResultObj != null) {
                tongueResult = new TongueResult();
                tongueResult.tongueDetectRes = tongueResultObj.optInt("tongueDetectRes");
                tongueResult.tongueCrack = tongueResultObj.optInt("tongueCrack");
                tongueResult.tongueFatThin = tongueResultObj.optInt("tongueFatThin");
                tongueResult.tongueNatureColor = tongueResultObj.optInt("tongueNatureColor");
                tongueResult.tongueCoatThickness = tongueResultObj.optInt("tongueCoatThickness");
                tongueResult.tongueCoatColor = tongueResultObj.optInt("tongueCoatColor");
                tongueResult.tongueThumbUrl = tongueResultObj.optString("tongueThumbUrl");
            }
        }
        return true;
    }


}
